using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace DAL
{
    public static partial class VideoTypeService
    {
        public static bool AddVideoType(VideoType videoType)
        {
            string sql =
                "INSERT VideoType (Title)" +
                "VALUES (@Title)";

            sql += " ; SELECT @@IDENTITY";


            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@Title", videoType.Title)
			};

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        public static bool DeleteVideoTypeByID(int iD)
        {
            string sql = "DELETE VideoType WHERE ID = @ID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", iD)
			};

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public static bool ModifyVideoType(VideoType videoType)
        {
            string sql =
                "UPDATE VideoType " +
                "SET " +
                    "Title = @Title " +
                "WHERE ID = @ID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", videoType.ID),
				new SqlParameter("@Title", videoType.Title)
			};

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static IList<VideoType> GetAllVideoTypes()
        {
            string sqlAll = "SELECT * FROM VideoType";
            return GetVideoTypesBySql(sqlAll);
        }

        public static VideoType GetVideoTypeByID(int iD)
        {
            string sql = "SELECT * FROM VideoType WHERE ID = @ID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", iD)
			};

            DataTable temp = DBHelper.ExecuteGetDataTable(CommandType.Text, sql, para);

            VideoType videoType = new VideoType();

            foreach (DataRow dr in temp.Rows)
            {
                videoType.ID = (int)dr["ID"];
                videoType.Title = (string)dr["Title"];

            }
            return videoType;

        }


        private static IList<VideoType> GetVideoTypesBySql(string safeSql)
        {
            List<VideoType> list = new List<VideoType>();

            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, null))
            {
                while (dr.Read())
                {
                    VideoType videoType = new VideoType();

                    videoType.ID = (int)dr["ID"];
                    videoType.Title = (string)dr["Title"];

                    list.Add(videoType);
                }
            }
            return list;
        }

        private static IList<VideoType> GetVideoTypesBySql(string safeSql, params SqlParameter[] para)
        {
            List<VideoType> list = new List<VideoType>();

            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, para))
            {
                while (dr.Read())
                {
                    VideoType videoType = new VideoType();

                    videoType.ID = (int)dr["ID"];
                    videoType.Title = (string)dr["Title"];

                    list.Add(videoType);
                }
            }
            return list;
        }
    }
}
